package com.shensi.appCollect.controller;

import com.shensi.appCollect.anno.SimpleRepeatedReqLimit;
import com.shensi.appCollect.enums.CardRecordType;
import com.shensi.appCollect.pojo.CustomerCardUseRecord;
import com.shensi.appCollect.pojo.dto.CustomerCardChangeDto;
import com.shensi.appCollect.service.ElectronicCardService;
import com.shensi.appCollect.utils.MathUtil;
import com.shensi.appCollect.utils.MobilePhoneUtil;
import com.shensi.appCollect.utils.ResultData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @date 2023/12/10
 */
@RestController
@CrossOrigin
public class ElectronicCardController {

    @Autowired
    private ElectronicCardService electronicCardService;

    @PostMapping("/electronicCard/posUpdateBalance")
    @ApiOperation(value = "pos机更新会员电子卡余额")
    @SimpleRepeatedReqLimit(name = "posUpdateBalance:",express = "[0].getMobilePhone()+':'+[0].getCardType()",expire = 25)
    public ResultData<CustomerCardUseRecord> posUpdateBalance(@RequestBody @Validated(CustomerCardChangeDto.PosView.class) CustomerCardChangeDto cardChangeDto){
        CustomerCardUseRecord useRecord = electronicCardService.posUpdateBalance(cardChangeDto);
        useRecord.setMobilePhone(MobilePhoneUtil.encryPhone(useRecord.getMobilePhone()));
        useRecord.setPayType(CardRecordType.getByKey(useRecord.getPayType(),CardRecordType.MANUAL_ADD).getName());
        useRecord.setIntegral(MathUtil.add(null,useRecord.getIntegral()));
        return ResultData.success(useRecord);
    }

    @PostMapping("/electronicCard/pcUpdateBalance")
    @ApiOperation(value = "pc更新会员电子卡余额")
    @SimpleRepeatedReqLimit(name = "pcUpdateBalance:",express = "[0].getId()",expire = 25)
    public ResultData<CustomerCardUseRecord> pcUpdateBalance(@RequestBody @Validated(CustomerCardChangeDto.PcView.class) CustomerCardChangeDto cardChangeDto){
        cardChangeDto.setPresentAmt(BigDecimal.ZERO);
        electronicCardService.pcUpdateBalance(cardChangeDto);
        return ResultData.success();
    }
}
